package ui.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityOptionsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.api.ApiConstant;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.NearHousingBean;
import com.fy.fyzf.bean.PassengerDetailBean;
import com.fy.fyzf.bean.PassengerListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SearchPassengerBean;
import com.fy.fyzf.listener.PermissionListener;
import com.fy.fyzf.presenter.PassengerPresnter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.ShareHelper;
import com.fy.fyzf.view.PassengerCallBack;
import com.fy.fyzf.view.SpacesItemDecoration;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import ui.adapter.HousesCollectionAdapter;
import ui.adapter.HousesListAdapter;

public class MyPassengerSourceActivity extends BaseActivity<PassengerPresnter> implements PassengerCallBack {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.leftView)
    TextView leftView;
    @BindView(R.id.tv_center_title)
    TextView tvCenterTitle;
    @BindView(R.id.rightImage)
    ImageView rightImage;
    @BindView(R.id.right)
    TextView right;
    @BindView(R.id.titleBar)
    RelativeLayout titleBar;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.titleLine)
    View titleLine;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_call)
    ImageView ivCall;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_match)
    TextView tvMatch;
    @BindView(R.id.tv_daikan)
    TextView tvDaikan;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.edit_demand)
    RelativeLayout editDemand;

    private int id;
    private HousesCollectionAdapter housesListAdapter;

    @Override
    protected PassengerPresnter createPresenter() {
        return new PassengerPresnter(this);
    }

    private int pageNumber = 1;
    private int tag = 1;
    private int pageSize = 10;

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_my_passenger_source;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("我的客源");
        housesListAdapter = new HousesCollectionAdapter(this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(housesListAdapter);
        recycleView.addItemDecoration(new SpacesItemDecoration(20));
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                tag = 1;
                pageNumber = 1;
                initData();
            }
        });

        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                tag = 2;
                pageNumber++;
                initData();
            }
        });

        housesListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<HousesListBean.ListBean> listBeans = adapter.getData();
                Intent intent = new Intent(MyPassengerSourceActivity.this, HouseDetailActivity.class);
                intent.putExtra("housingid", listBeans.get(position).getHousingId());
                intent.putExtra("type", 1);
                ImageView imageView = view.findViewById(R.id.iv_logo);
                startActivity(intent);
            }
        });
        housesListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<HousesListBean.ListBean> data = adapter.getData();
                String url = ApiConstant.BASE_SERVER_URL + "shareHousing/getHousing?housingId="+data.get(position).getHousingId();

                if (view.getId() == R.id.share_wx) {
                    ShareHelper shareHelper = new ShareHelper(MyPassengerSourceActivity.this);
                    shareHelper.shareWx(data.get(position).getHousingImage(), url);
                } else if (view.getId() == R.id.share_pyq) {
                    ShareHelper shareHelper = new ShareHelper(MyPassengerSourceActivity.this);
                    shareHelper.shareWechatMoments(data.get(position).getHousingImage(), url);
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        registerEventBus(this);
        id = getIntent().getIntExtra("id", -1);
        mPresenter.getPassengerDetail(id);
    }

    //collectionSuccess
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String s){
        if (s.equals("collectionSuccess")) {
            SearchPassengerBean searchPassengerBean = new SearchPassengerBean();
            searchPassengerBean.setPageSize(pageSize);
            searchPassengerBean.setPageNum(pageNumber);
            searchPassengerBean.setCustomerId(id);
            mPresenter.getCollectHousing(searchPassengerBean);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterEventBus(this);
    }

    @OnClick({R.id.edit_demand, R.id.iv_call, R.id.tv_match, R.id.tv_daikan, R.id.tv_sign})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_demand:
                Intent intent = new Intent(this, EditDemandActivity.class);
                intent.putExtra("data", passengerData);
                startActivity(intent);
                break;
            case R.id.iv_call:
                requestRuntimePermission(new String[]{Manifest.permission.CALL_PHONE}, new PermissionListener() {
                    @Override
                    public void onGranted() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tvTel.getText().toString()));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                    @Override
                    public void onDenied(List<String> deniedPermissions) {
                        AppUtils.showToast("拨号权限被拒绝");
                    }
                });

                break;
            case R.id.tv_match:
                intent = new Intent(MyPassengerSourceActivity.this, MatchHousingActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                break;
            case R.id.tv_daikan:
                intent = new Intent(MyPassengerSourceActivity.this, WithGlassesHousingActivity.class);
                intent.putExtra("customersId", customersId);
                startActivity(intent);
                break;
            case R.id.tv_sign:
                intent = new Intent(MyPassengerSourceActivity.this, SignContractActivity.class);
                intent.putExtra("customersId", customersId);
                startActivity(intent);

                break;
        }
    }

    @Override
    public void getPassengerListSuccess(PassengerListBean passengerListBean) {

    }

    private PassengerDetailBean passengerData;
    private int customersId;

    @Override
    public void getPassengerDetailSuccess(PassengerDetailBean passengerDetailBean) {
        smartRefresh.finishRefresh();
        smartRefresh.finishLoadMore();
        passengerData = passengerDetailBean;
        customersId = passengerData.getCustomerId();
//        GlideUtils.loadCircleImage(passengerDetailBean.);
        tvName.setText(passengerDetailBean.getCustomerName());
        tvTel.setText(passengerDetailBean.getCustomerMobile());
        tvPrice.setText(passengerDetailBean.getCustomerRange() + "元");
        tvArea.setText(passengerDetailBean.getCustomerArea() + "平米");
        tvLocation.setText(passengerDetailBean.getParentAreaName());
        //房源列表 带收藏标识接口 点匹配之后调用

        SearchPassengerBean searchPassengerBean = new SearchPassengerBean();
        searchPassengerBean.setPageSize(pageSize);
        searchPassengerBean.setPageNum(pageNumber);
        searchPassengerBean.setCustomerId(id);

        mPresenter.getCollectHousing(searchPassengerBean);
    }

    @Override
    public void getCollectHousingSuccess(HousesListBean housesListBean) {
        if (null == housesListBean || housesListBean.getList().size() <= 0) {
            housesListAdapter.setEmptyView(R.layout.layout_nodata_search, recycleView);
        }
        if (tag == 1) {
            housesListAdapter.setNewData(housesListBean.getList());
        } else {
            housesListAdapter.addData(housesListBean.getList());
        }
    }

    @Override
    public void getMatchHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void getRecommendHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void AddPassengerSourceSuccess(BaseData baseData) {

    }

    @Override
    public void getSiteListSuccess(List<AreaListBean> areaListBeans) {

    }

    @Override
    public void editPassengerSuccess(BaseData baseData) {

    }
}